package cn.edu.xmu.javaee.productthymleaf.controller;

import cn.edu.xmu.javaee.productthymleaf.dao.bo.Product;
import cn.edu.xmu.javaee.productthymleaf.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/products")
@RequiredArgsConstructor
@Slf4j
public class ProductController {

    private final ProductService productService;

    /**
     * 显示商品列表页面
     */
    @GetMapping
    public String listProducts(Model model) {
        // 模拟数据，实际项目中应该从数据库获取
        List<Product> products = List.of(
            Product.builder()
                .id(1L)
                .name("测试商品1")
                .originalPrice(100L)
                .originPlace("厦门")
                .categoryId(2L)
                .shopId(2L)
                .productId(100L)
                .build(),
            Product.builder()
                .id(2L)
                .name("测试商品2")
                .originalPrice(200L)
                .originPlace("北京")
                .categoryId(1L)
                .shopId(1L)
                .productId(101L)
                .build()
        );
        
        model.addAttribute("products", products);
        return "products/list";
    }

    /**
     * 显示商品详情页面
     */
    @GetMapping("/{id}")
    public String viewProduct(@PathVariable Long id, Model model) {
        try {
            Product product = productService.getDraft(2L, id);
            model.addAttribute("product", product);
            return "products/detail";
        } catch (Exception e) {
            log.error("获取商品详情失败: {}", e.getMessage());
            model.addAttribute("error", "商品不存在或无权访问");
            return "products/error";
        }
    }

    /**
     * 显示添加商品表单
     */
    @GetMapping("/new")
    public String showCreateForm(Model model) {
        model.addAttribute("product", new Product());
        return "products/form";
    }

    /**
     * 显示编辑商品表单
     */
    @GetMapping("/{id}/edit")
    public String showEditForm(@PathVariable Long id, Model model) {
        try {
            Product product = productService.getDraft(2L, id);
            model.addAttribute("product", product);
            return "products/form";
        } catch (Exception e) {
            log.error("获取商品信息失败: {}", e.getMessage());
            model.addAttribute("error", "商品不存在或无权访问");
            return "products/error";
        }
    }

    /**
     * 处理商品创建/更新
     */
    @PostMapping
    public String saveProduct(@ModelAttribute Product product) {
        try {
            if (product.getId() == null) {
                // 新建商品
                productService.createDraft(product);
            } else {
                // 更新商品
                productService.updateDraft(2L, product);
            }
            return "redirect:/products";
        } catch (Exception e) {
            log.error("保存商品失败: {}", e.getMessage());
            return "redirect:/products?error=保存失败";
        }
    }

    /**
     * 删除商品
     */
    @PostMapping("/{id}/delete")
    public String deleteProduct(@PathVariable Long id) {
        try {
            productService.delDraft(2L, id);
            return "redirect:/products";
        } catch (Exception e) {
            log.error("删除商品失败: {}", e.getMessage());
            return "redirect:/products?error=删除失败";
        }
    }
}
